package com.fjnu;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

@Configuration
@ComponentScan(basePackages = "com.fjnu")
@PropertySource("db.properties")
public class SpringConfig {

    @Value("${sqlserver.driver}")
    private String driver;

    @Value("${sqlserver.url}")
    private String url;

    @Value("${sqlserver.username}")
    private String username;

    @Value("${sqlserver.password}")
    private String password;

    @Bean
    public Connection connection() {
        try {
            Class.forName(driver);

            // 添加连接属性
            Properties props = new Properties();
            props.setProperty("user", username);
            props.setProperty("password", password);
            props.setProperty("encrypt", "false");
            props.setProperty("trustServerCertificate", "true");
            props.setProperty("sslProtocol", "TLSv1");
            props.setProperty("loginTimeout", "30");

            return DriverManager.getConnection(url, props);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}